package com.kaifamiao.socket;

import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;

public class FirstClient {

    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);

        final int port = 8888;
        final InetAddress address = InetAddress.getLocalHost();
        SocketAddress remoteAddress = new InetSocketAddress( address, port);

        SocketAddress clientAddress = new InetSocketAddress( address, 9999);

        Socket socket = new Socket();
        socket.bind( clientAddress );

        System.out.println( "准备连接服务器" );
        socket.connect( remoteAddress );
        System.out.println( "服务器连接成功" );

        // 获得可以向服务端输出数据的字节流
        OutputStream out = socket.getOutputStream();
        // 将字节输出流包装成字节打印流
        PrintStream ps = new PrintStream(out);

        while( true ) {
            System.out.println("请输入向服务器发送的信息");
            String content = sc.nextLine();
            if( "bye bye bye bye".equalsIgnoreCase(content) ) {
                break;
            }
            ps.println(content);
        }

        socket.close();
        sc.close();

    }

}
